exports.artSalesQuestionUpdate = (req, res) => {

    var bodyParams = req.routeInfo.bodyParams;

    var resultData = {};

    var openid = '';

    var u_id = 0;

    //添加回复
    function AddReply(cb) {

        var param = {
            update_where: bodyParams.update_where,
            sq_id: bodyParams.sq_id,
            sq_reply: bodyParams.sq_reply,
            sq_reply_time: bodyParams.sq_reply_time,
            sq_status: bodyParams.sq_status,
            sq_apply_knowledge_status: bodyParams.sq_apply_knowledge_status,
        };

        if(bodyParams.sq_type){
            param.sq_type = bodyParams.sq_type;
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artSalesQuestionUpdate.Request_Api_Url,
                method: 'POST',
                data: param
            },
            function (data) {

                resultData.updateResult = data.result;
                cb();

            }
        )
    }

    /*发私信 sendPrivateMessage */

    //查询反馈者id
    function searchSaleInfo(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artSalesQuestionSearch.Request_Api_Url + "/sq_id/" + bodyParams.sq_id,
                method: 'POST',
                data: {
                    fields: ['sq_uid'].join(',')
                }
            },
            function (data) {

                u_id = data.result.data[0].sq_uid;

                cb();

            }
        )
    }

    //查询销售openid
    function searchSaleFatherEnterpriseOpenid(cb) {

        console.log(CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_id/" + u_id);
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_id/" + u_id,
                method: 'POST',
                data: {
                    fields: ['u_openid'].join(',')
                }
            },
            function (data) {

                openid = data.result.data[0].u_openid;

                cb();

            }
        )
    }

    //发私信
    function sendPrivateMessage(cb) {
    	console.log(openid)
    	var replyArr = JSON.parse(bodyParams.sq_reply);
    	var replyTxt = '';
    	for(var ss=0;ss<replyArr.length;ss++){
    		if(replyArr[ss].type == '0'){
    			replyTxt += replyArr[ss].txt;
    		}
    	}
    	if(replyTxt == ''){
    		replyTxt = '暂无文字内容';
    	}
    	
    	
    	if(openid == 0 || openid == ''){
    		cb();
    	}else{
			//添加反馈回复
	    	var params = {
				type : "fanKuiHuiFu",
	    		openid : openid,
	    		goUrls : "",//跳转
	    		content : bodyParams.sq_title, //反馈内容
	    		sq_id : bodyParams.sq_id, //反馈内容ID
	    		reply : replyTxt, //反馈回复
	    	}
	
	        R.SEND_HTTP(
	            req,
	            res,
	            {
	                url: config.server.ip + ":" + config.server.port + CORE_APIConfig.coreAPI.templateMessagesSent.Request_Api_Name,
	                method: 'POST',
	                data: params
	            },
	            function (data) {
	            	
	            	console.log(data);
	
	                cb();
	
	            }
	        )
	        
	    }
    }


    F.Serial([
        AddReply,
        searchSaleInfo,
        searchSaleFatherEnterpriseOpenid,
        sendPrivateMessage
    ], () => {
        RES.successResponse(res, resultData);
    })


}